<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\admin\controller;
use app\admin\model\Adminperiphery;
use app\common\builder\ZBuilder;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Scenicspot as ScenicspotModel;
use app\admin\model\Adminactivity as AdminactivityModel;
use think\Hook;

/**
 * 系统模块控制器
 * @package app\admin\controller
 */
class Relatedsettings extends Admin
{
    /**
     * 系统设置
     * @param string $group 分组
     * @author 蔡伟明 <314013107@qq.com>
     * @return mixed
     */
    public function index(){
        // 保存数据
        $typearr = ['仅展示','进入活动','进入景区'];//0什么都不做 1跳转url 2跳转指定活动 3跳转指定视频
        // 查询
        $map = $this->getMap();
        $order = $this->getOrder( 'solid asc,id desc');

        // 排序
        // 数据列表
        $map[] = ['del','=',0];
        $data_list =  BannerModel::getAll($map,'*',$order);
        //$data_list = BannerModel::where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '编辑',
            'icon'  => 'fa fa-pencil',
            'href'  => url('edit', ['id' => '__id__','aaa'=>'1','type'=>'0'])
        ];

        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('edit', ['id' => '__id__','type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加轮播图',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('edit')
        ];

        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('首页轮播图') // 设置页面标题
            ->setSearchArea([
                ['text:2', 'title', '描述','like'],
                ['select:2', 'type', '类型','','',$typearr],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['title', '描述'],
                ['pic','展示图','picture'],
                ['type','类型','status','',$typearr],
                ['add_time', '添加时间', ],
                ['right_button', '操作', 'btn']
            ])

//            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
            ->addTopButton('add', $btn_access,['area'=>['70%','80%']]) // 添加授权按钮
            ->addRightButton('editcode',$btn_accessedit,['area'=>['800px','80%']]) // 批量添加右侧按钮
            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    public function edit($id = 0,$aaa = '1',$type=0)
    {

        if($type == '2'){
            BannerModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }
        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            unset($data['__token__']);
            $data['type'] = $data['types'];
            $data['pic_url'] = get_file_path($data['pic']);
            unset($data['types']);

            $where = '';
            if($data['type'] == 2){
                $data['sid'] = $data['sid1'];
            }

            if($data['id'] != '' && $data['id'] !='0' && !empty($data['id'])){
                $where = 'id='.$data['id'];
            }else{
                $data['add_uid'] =  UID;
                $data['add_time'] =  date('Y-m-d H:i:s');
            }
            BannerModel::saveData($data,$where);
            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }
        // 获取数据
        $info = BannerModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单


        //需要选择是哪个景区
        $Scenicspotlist = ScenicspotModel::getApiData(['del'=>0],'id,title');
        $jingqulist = array();
        foreach ($Scenicspotlist as $k=>$v){
            $jingqulist[$v['id']] = $v['title'];
        }


        //需要选择是哪个活动
        $AdminactivityModellist = AdminactivityModel::getApiData(['del'=>0],'id,title');
        $huodong = array();
        foreach ($AdminactivityModellist as $k=>$v){
            $huodong[$v['id']] = $v['title'];
        }

        //展示视频上不了的
        if(!empty($info['type'])){
            $info['sid1'] = $info['sid'];
            $info['types'] = $info['type'];
        }
            return ZBuilder::make('form')
                ->setPageTitle('编辑') // 设置页面标题
                ->addFormItems([ // 批量添加表单项
                    ['hidden', 'id'],
                    ['text','title', '轮播图描述'],
                    ['select','types', '类型','',['仅展示','进入活动','进入景区']],
                    ['select','sid', '请选择对应活动','',$huodong],
                    ['select','sid1', '请选择对应景区','',$jingqulist],
                    ['image','pic','展示图'],
                    ['text','solid','排序'],
                ])
                ->setFormData($info) // 设置表单数据
                ->setTrigger('types', '1', 'sid')
                ->setTrigger('types', '2', 'sid1')
                ->fetch();
    }

    //景区的 需要有 餐饮 住宿  和 宿营地  //图片 + getApiData
    public function periphery($pid){
        // 查询
        $map = $this->getMap();
        // 排序
        // 数据列表
        $order = $this->getOrder( 'solid asc');
        $map[] = ['del','=',0];

        $data_list =  AdminperipheryModel::getAll($map,'*',$order);
        //$data_list = BannerModel::where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '修改',
            'icon'  => 'fa fa-pencil',
            'href'  => url('peripheryedit', ['id' => '__id__','pid'=>$pid,'type'=>'0'])
        ];

        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('peripheryedit', ['id' => '__id__','pid'=>$pid,'type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加周边信息',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('peripheryedit',['id' => '0','pid'=>$pid,'type'=>'0'])
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('周边') // 设置页面标题
            ->setSearchArea([
                ['select:2', 'score_flag', '是否使用积分', '', '', ['送积分','不送积分']],
                ['text:2', 'title', '周边名称','like'],
                ['text:2', 'desc', '周边描述','like'],
                ['text:2', 'map_address', '周边地址','like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['title', '名称'],
                ['uname', '添加人'],
                ['pic','展示图','picture'],
                ['money','价格'],
                ['score_flag','是否送积分','status','',['不送积分','送积分']],
                ['map_address','详细地址'],
                ['add_time', '添加时间', ],
                ['right_button', '操作', 'btn']
            ])

            ->addTopButton('add',$btn_access,['area'=>['800px','80%']]) // 批量添加顶部按钮
            ->addRightButton('editcode',$btn_accessedit,['area'=>['800px','80%']]) // 批量添加右侧按钮

            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    public function peripheryedit($id=0,$pid,$type=1)
    {
        if($type == '2'){
            AdminperipheryModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }

        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            $data['type'] = implode(',',$data['type']);
            unset($data['__token__']);
            $xmlData = file_get_contents('https://restapi.amap.com/v3/geocode/regeo?output=xml&location='.$data['map'].'&key=f79f23aae7273821ad7b3a7d85fbc9d1');
            //将xml格式转换为数组
            $dizhidata = FromXml($xmlData);
            if ($dizhidata['info'] != 'OK') {
                return json(array('code'=>'1','msg'=>'参数错误！'));
            }
            $regeocode = $dizhidata['regeocode']['addressComponent'];
            $data['province'] = $regeocode['province'];
            $data['city'] = $regeocode['city'];
            $data['area'] = $regeocode['district'];
            $data['province_id'] = db('china')->where(['name'=>$data['province']])->value('id');
            $data['city_id'] = db('china')->where(['name'=>$data['city']])->value('id');
            $data['area_id'] =db('china')->where(['name'=>$data['area']])->value('id');
            $userinfo = session('user_auth');

            $data['pid'] = $pid;
            $data['uname'] =    $userinfo['nickname'];
            $data['umobile'] =    $userinfo['mobile'];
            $data['content'] =    $_POST['content'];
            $data['pic_url'] =    get_file_path($data['pic']);


            $data['pic_top_url'] =   get_file_path($data['pic_top']);
            $where = '';

            if($data['id'] != '' && $data['id'] !='0' && !empty($data['id'])){
                $where = 'id='.$data['id'];
            }else{
                $data['add_time'] =  date('Y-m-d H:i:s');
                $data['add_uid'] =  UID;
            }
            AdminperipheryModel::saveData($data,$where);

            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }
        $scenic_spot_arr = config('periphery');
        // 获取数据
        $info = AdminperipheryModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单

        return ZBuilder::make('form')
            ->setPageTitle('周边信息') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['hidden', 'pid'],
                ['static','add_time','添加时间'],
                ['text','title', '名称'],
                ['image','pic','展示图'],
                ['images','pic_top','头部轮播图'],
                ['text','money','价格'],
                ['text','phone','联系电话'],
                ['select','score_flag','是否送积分','',['不送积分','送积分']],
                ['checkbox','type','景区类型','',$scenic_spot_arr],
                ['text','solid','排序'],
                ['textarea','desc','周边简介'],
                ['ueditor','content','详情'],
            ])
            ->addBmap('map', '地图', 'k5X4mt6dDU3VDvgvu4MzkR6GOOESVvo0')
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }

    //活动相册
    public function album($id){
        // 保存数据
        // 查询
        $map = $this->getMap();
        $order = $this->getOrder( 'solid asc');
        // 排序
        $map[] = ['del','=',0];
        $map[] = ['hid','=',$id];
        $data_list =  ActivityalbumModel::getAll($map,'*',$order);
        //$data_list = BannerModel::where($map)->order('id desc')->paginate();
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '编辑',
            'icon'  => 'fa fa-pencil',
            'href'  => url('albumedit', ['id' => '__id__','hid'=>$id,'type'=>'0'])
        ];
        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('albumedit', ['id' => '__id__','hid'=>$id,'type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加照片',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('albumedit',['id' => '__id__','hid'=>$id,'type'=>'2'])
        ];


        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('照片新') // 设置页面标题
            ->setSearchArea([
//                ['text:2', 'title', '照片描述','like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['files','展示图','picture'],
                ['add_time','添加时间'],
                ['right_button', '操作', 'btn']
            ])
//            ->addTopButtons('add,enable,disable,delete') // 批量添加顶部按钮
            ->addTopButton('add', $btn_access) // 添加授权按钮
            ->addRightButton('editcode',$btn_accessedit,['area'=>['800px','80%']]) // 批量添加右侧按钮
            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    public function albumedit($id = 0,$hid,$type=0)
    {
        if($type == '2'){
            ActivityalbumModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }
        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            unset($data['__token__']);
            $imgpath = array();
            $imgas = explode(',',$data['icons']);
            if($data['id'] != '' && $data['id'] !='0' && !empty($data['id'])){
                $where = 'id='.$data['id'];
            }
                $data['add_time'] = date('Y-m-d H:i:s');

                $data['file_path'] = get_file_path($data['files']);
                $data['hid'] = $hid;
                ActivityalbumModel::saveData($data,$where);
            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }
        $scenic_spot_arr = config('scenic_spot_arr');
        // 获取数据
        $info = ActivityalbumModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单
        //需要选择是哪个景区
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden', 'id'],
                ['image','icons','封面'],
                ['file_url','file_url','视频路径'],
                ['text','solid', '排序'],
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }


    //活动视频
    public function video($id){
        // 保存数据
        // 查询
        $map = $this->getMap();
        $order = $this->getOrder( 'solid asc');
        // 排序
        $map[] = ['del','=',0];
        $map[] = ['hid','=',$id];
        $data_list =  ActivityvideoModel::getAll($map,'*',$order);
        // 分页数据
        $page = $data_list->render();
        $btn_accessedit = [
            'title' => '编辑',
            'icon'  => 'fa fa-pencil',
            'href'  => url('videoedit',  ['id' => '__id__','hid'=>$id,'type'=>'1'])
        ];
        $btn_accessedl = [
            'title' => '删除',
            'icon'  => 'fa fa-times',
            'class'=> 'btn btn-xs btn-default ajax-get confirm',
            'href'  => url('videoedit',  ['id' => '__id__','hid'=>$id,'type'=>'2'])
        ];
        $btn_access = [
            'title' => '添加',
            'icon'  => 'fa fa-plus-circle',
            'class'=>"btn btn-primary",
            'href'  => url('videoedit', ['id' => '0','hid'=>$id,'type'=>'1'])
        ];
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('table')
            ->setPageTitle('活动信息') // 设置页面标题
            ->setSearchArea([
                ['text:2', 'title', '视频名称','like'],
            ])
            ->hideCheckbox()
            ->addColumns([ // 批量添加数据列
                ['id', '编号'],
                ['title', '视频名称'],
                ['files','视频封面图','picture'],
                ['file_url','视频地址'],
                ['right_button', '操作', 'btn']
            ])
            ->addTopButton('add', $btn_access) // 添加授权按钮
            ->addRightButton('editcode',$btn_accessedit) // 批量添加右侧按钮
            ->addRightButton('del',$btn_accessedl) // 批量添加右侧按钮
            ->setRowList($data_list) // edit
            ->setPages($page) // 设置分页数据
            ->fetch(); // 渲染页面
    }

    //相关操作的呢
    public function videoedit($id = 0,$hid,$type=0){
        if($type == '2'){
            ActivityvideoModel::saveData(['del'=>1],['id'=>$id]);
            $this->success('操作成功');
        }
        // 保存数据
        if ($this->request->isPost()) {
            $data = input('post.');
            unset($data['__token__']);
            $where = '';
            if($data['id'] != '' && $data['id'] !='0' && !empty($data['id'])){
                $where = 'id='.$data['id'];
            }
                $data['add_time'] = date('Y-m-d H:i:s');
                $data['hid'] = $hid;
                $data['file_path'] = get_file_path($data['files']);
            ActivityvideoModel::saveData($data,$where);

            $this->success('编辑成功','',['_close_pop' => 1,'_parent_reload' => 1]);
        }

        // 获取数据
        $info = ActivityvideoModel::getInfo('id='.$id,'*');
        // 使用ZBuilder快速创建表单
        //需要选择是哪个景区
        return ZBuilder::make('form')
            ->setPageTitle('编辑') // 设置页面标题
            ->addFormItems([ // 批量添加表单项
                ['hidden','id'],
                ['text','title','视频名称','视频名称'],
                ['image','files','视频封面图'],
                ['text','file_url','视频地址'],
                ['text','solid', '排序'],
            ])
            ->setFormData($info) // 设置表单数据
            ->fetch();
    }



}